package cn.initcap.algorithm.swing.sort.insertion;

import java.util.Arrays;
import java.util.Random;

/**
 * @author initcap
 * @date 8/1/19 3:20 PM
 */
public class InsertionSortData {

    /**
     * [0...orderedIndex) 是有序的
     */
    public int orderedIndex;
    public int currentIndex;
    private int[] numbers;

    public InsertionSortData(int n, int randomBound, Type dataType) {

        numbers = new int[n];

        for (int i = 0; i < n; i++) {
            numbers[i] = (int) (Math.random() * randomBound) + 1;
        }

        if (dataType == Type.NearlyOrdered) {
            Arrays.sort(numbers);
            int swapTime = (int) (0.02 * n);
            for (int i = 0; i < swapTime; i++) {
                int a = new Random().nextInt(n);
                int b = new Random().nextInt(n);
                swap(a, b);
            }
        }
    }

    public InsertionSortData(int n, int randomBound) {
        this(n, randomBound, Type.Default);
    }

    public int N() {
        return numbers.length;
    }

    public int get(int index) {
        if (index < 0 || index >= numbers.length) {
            throw new IllegalArgumentException("Invalid index to access Sort Data.");
        }

        return numbers[index];
    }

    public void swap(int i, int j) {
        if (i < 0 || i >= numbers.length || j < 0 || j >= numbers.length) {
            throw new IllegalArgumentException("Invalid index to access Sort Data.");
        }

        int t = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = t;
    }

    public enum Type {
        Default,
        NearlyOrdered
    }
}
